<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page="page" :permission="permissionList"
               :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate" @row-save="rowSave"
               @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange" @size-change="sizeChange" @on-load="onLoad">
      <!-- <template slot="menuLeft">
          <el-button type="danger" size="small" icon="el-icon-delete" plain v-if="permission.bj_delete"
              @click="handleDelete">删 除
          </el-button>
      </template> -->
      <!-- <template slot-scope="{type,size,row,index}" slot="menuBtn">
          <el-dropdown-item divided @click.native="showJobRecords(row, index)">上传文件</el-dropdown-item>
      </template> -->
    </avue-crud>
    <el-dialog title="上传文件" :visible.sync="dialogVisible" width="30%" center :before-close="handleClose">
      <avue-form :option="upOption" v-model="upform">
      </avue-form>
      <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitrep">确 定</el-button>
            </span>
    </el-dialog>
  </basic-container>
</template>

<script>
import {getList, getDetail, add, update, remove, jsList, GetFileUploadInfo} from "@/api/service/jzhy";
import {mapGetters} from "vuex";
import {school} from "@/api/service/jyhy";
// import da from "element-ui/src/locale/lang/da";

export default {
  data() {
    return {
      form: {},
      query: {},
      dialogVisible: false,
      isThrottle: false,
      typeRdeio: 1,
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 7,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        labelWidth: 100,
        // menuType: 'menu',
        // menuBtnTitle: '更多操作',
        column: [
          {
            label: "主键",
            prop: "id",
            rules: [{
              required: true,
              message: "请输入主键",
              trigger: "blur"
            }],
            hide: true,
            display: false
          },
          {
            label: "学校名称",
            prop: "schoolId",
            search: false,
            rules: [{
              required: true,
              message: "请输入学校名称",
              trigger: "blur"
            }],
            type: "tree",
            dicUrl: "/api/PcPublicService/GetSchoolTreeSelect",
            props: {
              label: "xxName",
              value: "id"
            },
          },
          {
            label: "会议主题",
            prop: "name",
            maxlength: 20,
            showWordLimit: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入会议主题",
              trigger: "blur"
            }],
          },
          {
            label: "开始时间",
            prop: "startTime",
            // search: true,
            type: "datetime",
            format: "yyyy-MM-dd HH:mm",
            valueFormat: "yyyy-MM-dd HH:mm",
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择开始时间",
              trigger: "blur"
            }]
          },
          {
            label: "会议类型",
            prop: "mode",
            type: 'checkbox',
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择会议类型",
              trigger: "blur"
            }],
            hide: true,
            dicData: [{
              label: '参会家长',
              value: 1
            }, {
              label: '参会老师',
              value: 2
            }],
            change: (e) => {
              this.participant(e)
            },
          },
          {
            label: "班级",
            prop: "bjId",
            overHidden: true,
            rules: [{
              required: false,
              message: "请选中班级",
              trigger: "blur"
            }],
            hide: true,
            display: false,
            type: 'select',
            dicData: [],
            props: {
              label: "name",
              value: "id"
            },
          },
          {
            label: "参会方",
            prop: "ParticipantId",
            overHidden: true,
            rules: [{
              required: false,
              message: "请选择参会方",
              trigger: "blur"
            }],
            hide: true,
            display: false,
            type: 'select',
            dicData: [],
            multiple: true,
            props: {
              label: "name",
              value: "id"
            },
          },
          // {
          //     label: "人数",
          //     prop: "leverage",
          //     overHidden: true,
          //     rules: [{
          //         required: true,
          //         message: "请输入人数",
          //         trigger: "blur"
          //     }],
          // },
          {
            label: "会议时长",
            prop: "hour",
            overHidden: true,
            type: 'number',
            rules: [{
              required: true,
              message: "请输入会议时长",
              trigger: "blur"
            }],
          },
          {
            label: "会议描述",
            prop: "description",
            type: "textarea",
            span: 24,
            maxlength: 800,
            showWordLimit: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入会议描述",
              trigger: "blur"
            }]
          },
        ]
      },
      upform: [],
      upOption: {
        labelWidth: 120,
        column: [
          {
            label: '数组对象',
            prop: 'array',
            dataType: 'object',
            type: 'upload',
            propsHttp: {
              res: 'data'
            },
            span: 24,
            action: ''
          },]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission", "JSuserInfo"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.bj_add, false),
        viewBtn: this.vaildData(this.permission.bj_view, false),
        delBtn: this.vaildData(this.permission.bj_delete, false),
        editBtn: false
      };
    },
    schoolId() {
      return this.JSuserInfo.schoolId
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  mounted() {
    this.getSchool()
  },
  methods: {
    participant(data) {
      console.log('11111111', data.value)
      if (this.isThrottle || data.value == undefined) {
        return;
      }
      this.isThrottle = true
      let bjIdNames = this.findObject(this.option.column, "bjId");
      let ParticipantIds = this.findObject(this.option.column, "ParticipantId");
      let type = 2
      let patriarchType = data.value.indexOf(1)
      let teacherType = data.value.indexOf(2)
      let id = this.form.schoolId
      //types  类别 2学校,3年级,4班级 7教师 8学生
      if (teacherType >= 0) {
        type = 7
        jsList(type, id).then(res => {
          ParticipantIds.display = true
          ParticipantIds.dicData = res.data.data;
          this.isThrottle = false
        })
      } else {
        this.form.ParticipantId = ''
        ParticipantIds.display = false
        this.isThrottle = false
      }
      if (patriarchType >= 0) {
        type = 4
        jsList(type, id).then(res => {
          bjIdNames.display = true
          bjIdNames.dicData = res.data.data;
          this.isThrottle = false
        })
      } else {
        this.form.bjId = ''
        bjIdNames.display = false
        this.isThrottle = false
      }
    },
    //上传文件
    showJobRecords(row, index) {
      console.log(index)
      this.dialogVisible = true
      GetFileUploadInfo(row.id).then(res => {
        console.log(res.data.data)
        let array = this.findObject(this.upOption.column, "array");
        array.action = res.data.data.url
      })
    },
    rowSave(row, done, loading) {
      row.id = '0'
      if (this.form.mode === 1 && this.form.bjId === '') {
        this.$message({
          type: "warning",
          message: "请选择班级!"
        });
        return 0
      }
      row.bjId = row.bjId === '' ? '0' : row.bjId
      // row.schoolId = this.schoolId
      let mode = row.mode
      if (mode.indexOf(1) >= 0 && mode.indexOf(2) < 0) {
        row.mode = 1
      }
      if (mode.indexOf(1) < 0 && mode.indexOf(2) >= 0) {
        row.mode = 2
      }
      if (mode.indexOf(1) >= 0 && mode.indexOf(2) >= 0) {
        row.mode = 3
      }
      // if (row.jSDtos.length > 0) {
      //     row.ParticipantId = row.jSDtos.toString()
      // } else {
      //     row.ParticipantId = row.participantName
      // }
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      row.bjId = row.bjId === '' ? '0' : row.bjId
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({id: row.id});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({id: this.ids});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          console.log('11111', res)
          if (res.data.data !== null) {
            this.form = res.data.data;
            this.form.bjId = this.form.bjName
            let data = res.data.data
            this.form.mode = data.mode === 1 ? [1] : data.mode === 2 ? [2] : [1, 2]
            if (data.releaseDtos.length > 0) {
              let teacherName = []
              data.releaseDtos.forEach(item => {
                teacherName.push(item.name)
              })
              console.log('teacherName', teacherName)
              this.form.ParticipantId = teacherName.toString()
            }
          }
        });
        // let bjIdNames = this.findObject(this.option.column, "bjId");
        // bjIdNames.display = true
        // this.form.mode = [2]
        // console.log('1111111111')

      }
      if (["add"].includes(type)) {
        this.getSchool()
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      // this.query.schoolId = this.schoolId
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    getSchool() {
      school().then(res => {
        if (res.data.data !== null && res.data.data.length > 0) {
          this.form.schoolId = res.data.data[0].id
        }
      });
    }

  }
};
</script>

<style></style>
